package org.credo.net;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.HttpURLConnection;

public class ImitateOpera {

	public static void main(String[] args) {
		System.out.println("开始main方法:");
		ImitateOpera test = new ImitateOpera();
		test.sendPostToRest();
	}
	
	public void sendPostToRest() {
		try {
			System.out.println("开始try:"+ImitateOpera.class);
			String xmlpath = "D:\\restxml\\stay.xml";// xml文件保存路径
			// 读取xml文件
			BufferedReader fin = new BufferedReader(new FileReader(xmlpath));
			StringBuffer xmlBuffer = new StringBuffer();
			int k = 0;
			while (k != -1) {
				k = fin.read();
				if (k != -1)
					xmlBuffer.append((char) k);
			}
			fin.close();
			System.out.println("XML内容为:"+xmlBuffer.toString());
			System.out.println("发送xml文件");
			String xmlStr = xmlBuffer.toString();
			//String url2 = "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=RATE&status=123";
			String url2 = "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=stay&status=123";
			//String url2 = "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=RESERVATION&status=123";
			java.net.URL pageUrl = new java.net.URL(url2);
			HttpURLConnection httpurlconnection = (HttpURLConnection) pageUrl.openConnection();
			httpurlconnection.setRequestMethod("POST");
			httpurlconnection.setDoInput(true);
			httpurlconnection.setDoOutput(true);
			
			httpurlconnection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
			httpurlconnection.setRequestProperty("Pragma", "no-cache");
			httpurlconnection.setRequestProperty("Expires", "-1");
			
			httpurlconnection.setRequestProperty("Content-Length", "" + xmlStr.getBytes().length);
			System.out.println("lengthNum:"+xmlStr.getBytes().length);
			
		    httpurlconnection.getOutputStream().write(xmlStr.getBytes());
		    
			httpurlconnection.getOutputStream().flush();
		    httpurlconnection.getOutputStream().close();
			BufferedInputStream bis = new BufferedInputStream(httpurlconnection.getInputStream());
			bis.close();
			xmlBuffer = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
